package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.Language;
import util.enumeration.PriceRange;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;

@FacesConverter(value = "priceRangeConverter", forClass = util.enumeration.PriceRange.class)
public class PriceRangeConverter implements Converter {
    private WebLocalization localization;
    private String preferredLocale;
    
    public PriceRangeConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {    

        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null || value.length() == 0) {
            return null;
        }
    
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getPriceRangeAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getPriceRangeAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getPriceRangeAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getPriceRangeAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getPriceRangeAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }
        
        if (value instanceof PriceRange) {
            PriceRange priceRange = (PriceRange) value;
            
            String localizedPriceRange = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedPriceRange = getPriceRangeAsString_EN_US(priceRange);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedPriceRange = getPriceRangeAsString_EN_SG(priceRange);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedPriceRange = getPriceRangeAsString_MS_MY(priceRange);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedPriceRange = getPriceRangeAsString_ZH_CN(priceRange);
            }
            
            // other language...
            else{
                localizedPriceRange = getPriceRangeAsString_EN_US(priceRange);
            }

            return localizedPriceRange;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.PriceRange");
        }
    }
    
    // simplified chinese (zh_CN)
    private static Object getPriceRangeAsObject_ZH_CN(String value) {
        if(value.equals("低于二十新元")) return PriceRange.LESSTHAN$20;
        else if(value.equals("二十新元 至 四十新元")) return PriceRange.$20to$40;
        else if(value.equals("四十一新元 至 六十新元")) return PriceRange.$41to$60;
        else if(value.equals("六十一新元 至 八十新元")) return PriceRange.$61to$80;
        else if(value.equals("高于一百新元")) return PriceRange.MORETHAN$100;
        else return null;
    }
    
    // default (en_US)
    private static Object getPriceRangeAsObject_EN_US(String value) {
        if(value.equals("LESS THAN $20")) return PriceRange.LESSTHAN$20;
        else if(value.equals("$20 to $40")) return PriceRange.$20to$40;
        else if(value.equals("$41 to $60")) return PriceRange.$41to$60;
        else if(value.equals("$61 to $80")) return PriceRange.$61to$80;
        else if(value.equals("MORE THAN $100")) return PriceRange.MORETHAN$100;
        else return null;
    }
    
    // singapore english (en_SG)
    private static Object getPriceRangeAsObject_EN_SG(String value) {
        if(value.equals("LESS THAN $20")) return PriceRange.LESSTHAN$20;
        else if(value.equals("$20 to $40")) return PriceRange.$20to$40;
        else if(value.equals("$41 to $60")) return PriceRange.$41to$60;
        else if(value.equals("$61 to $80")) return PriceRange.$61to$80;
        else if(value.equals("MORE THAN $100")) return PriceRange.MORETHAN$100;
        else return null;
    }

    // bahasa malaysia (ms_MY)
    private static Object getPriceRangeAsObject_MS_MY(String value) {
        if(value.equals("kurang daripada 20 dolar")) return PriceRange.LESSTHAN$20;
        else if(value.equals("20 hingga 40 dolar")) return PriceRange.$20to$40;
        else if(value.equals("41 hingga 60 dolar")) return PriceRange.$41to$60;
        else if(value.equals("61 hingga 80 dolar")) return PriceRange.$61to$80;
        else if(value.equals("lebih daripada 100 dolar")) return PriceRange.MORETHAN$100;
        else return null;
    }

    
    // simplified chinese (zh_CN)
    private static String getPriceRangeAsString_ZH_CN(PriceRange priceRange) {
        String localizedPriceRange = "";
        
        switch (priceRange) {
            case LESSTHAN$20: localizedPriceRange = "低于二十新元" ; break; 
            case $20to$40: localizedPriceRange = "二十新元 至 四十新元"; break;
            case $41to$60: localizedPriceRange = "四十一新元 至 六十新元"; break;
            case $61to$80: localizedPriceRange = "六十一新元 至 八十新元"; break;
            case MORETHAN$100: localizedPriceRange = "高于一百新元"; break;
        }
        return localizedPriceRange;
    }
    
    // default (en_US)
    private static String getPriceRangeAsString_EN_US(PriceRange priceRange) {
        String localizedPriceRange = "";
        
        switch (priceRange) {
            case LESSTHAN$20: localizedPriceRange = "LESS THAN $20" ; break;
            case $20to$40: localizedPriceRange = "$20 to $40"; break;
            case $41to$60: localizedPriceRange = "$41 to $60"; break;
            case $61to$80: localizedPriceRange = "$61 to $80"; break;
            case MORETHAN$100: localizedPriceRange = "MORE THAN $100"; break;
        }
        return localizedPriceRange;
    }
    
    // singapore english (en_SG)
    private static String getPriceRangeAsString_EN_SG(PriceRange priceRange) {
        String localizedPriceRange = "";
        
        switch (priceRange) {
            case LESSTHAN$20: localizedPriceRange = "LESS THAN $20" ; break;
            case $20to$40: localizedPriceRange = "$20 to $40"; break;
            case $41to$60: localizedPriceRange = "$41 to $60"; break;
            case $61to$80: localizedPriceRange = "$61 to $80"; break;
            case MORETHAN$100: localizedPriceRange = "MORE THAN $100"; break;
        }
        return localizedPriceRange;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getPriceRangeAsString_MS_MY(PriceRange priceRange) {
        String localizedPriceRange = "";
        
        switch (priceRange) {
            case LESSTHAN$20: localizedPriceRange = "kurang daripada 20 dolar" ; break;
            case $20to$40: localizedPriceRange = "20 hingga 40 dolar"; break;
            case $41to$60: localizedPriceRange = "41 hingga 60 dolar"; break;
            case $61to$80: localizedPriceRange = "61 hingga 80 dolar"; break;
            case MORETHAN$100: localizedPriceRange = "lebih daripada 100 dolar"; break;
        }
        return localizedPriceRange;
    }
}